package org.duang.logs.db.mongodb;

import org.duang.logs.db.ToolUtils;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;

public class MongodbLogBackLogger  extends org.duang.logs.Logger {
	private Logger logger;
	
	MongodbLogBackLogger(Class<?> clazz) {
		logger = (Logger)LoggerFactory.getLogger(clazz);
		LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
		MongoDBAppender<ILoggingEvent>  mongoAppender = new MongoDBAppender<ILoggingEvent>();
		mongoAppender.setContext(loggerContext);
		mongoAppender.start();
		logger.addAppender(mongoAppender);
		logger.setLevel(ToolUtils.getLevel());
		logger.setAdditive(ToolUtils.isAdditive());  //不继承Root的appender, 即是否在控制台输出,true为输出
	}
	
	@Override
	public void print(String msg) {
		if (logger.isDebugEnabled()) {
			logger.debug(msg);
		} else if (logger.isInfoEnabled()) {
			logger.info(msg);
		} else if (logger.isWarnEnabled()) {
			logger.warn(msg);
		} else if (logger.isErrorEnabled()) {
			logger.error(msg);
		}
	}

	@Override
	public void print(String msg, Throwable t) {
		if (logger.isDebugEnabled()) {
			logger.debug(msg, t);
		} else if (logger.isInfoEnabled()) {
			logger.info(msg, t);
		} else if (logger.isWarnEnabled()) {
			logger.warn(msg, t);
		} else if (logger.isErrorEnabled()) {
			logger.error(msg, t);
		}
	}
	
	@Override
	public boolean isDebugEnabled() {
		return logger.isDebugEnabled();
	}

	@Override
	public boolean isInfoEnabled() {
		return logger.isInfoEnabled();
	}

	@Override
	public boolean isWarnEnabled() {
		return logger.isWarnEnabled();
	}

	@Override
	public boolean isErrorEnabled() {
		return logger.isErrorEnabled();
	}
}
